package com.lul15834.orderfood.activity.user.fragment;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ListView;

import androidx.appcompat.widget.SearchView;
import androidx.fragment.app.Fragment;

import com.lul15834.orderfood.R;
import com.lul15834.orderfood.activity.user.ManageUser;
import com.lul15834.orderfood.activity.user.adapter.UserAdapter;
import com.lul15834.orderfood.activity.user.adapter.UserNoAdapter;
import com.lul15834.orderfood.bean.OrderBean;
import com.lul15834.orderfood.dao.OrderDao;
import com.lul15834.orderfood.until.Tools;

import java.util.List;


public class UserFinishFragment extends Fragment {
    View rootview;//根视图

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        rootview=inflater.inflate(R.layout.fragment_user_order_finish, container, false);
        ListView listView=rootview.findViewById(R.id.user_order_listView);
        ManageUser man=(ManageUser)getActivity();
        ImageView back=rootview.findViewById(R.id.user_order_back);
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                man.showMy();
            }
        });
        String account= Tools.getOnAccount(rootview.getContext());
        String sta="1";
        List<OrderBean> list= OrderDao.getAllOrderByStaAndUserFinish(account,sta);
        UserAdapter orderNoAdapter=new UserAdapter(rootview.getContext(),list);
        if (list==null||list.size()==0){
            listView.setAdapter(null);
        }else {
            listView.setAdapter(orderNoAdapter);
        }
        SearchView searchView=rootview.findViewById(R.id.user_order_search);
        searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener()  {
            @Override
            public boolean onQueryTextSubmit(String query) {
                List<OrderBean> list=OrderDao.getAllOrderByStaAndUserFinish(account,sta);
                List<OrderBean> list1=Tools.filterOrder(list,query);
                UserAdapter orderNoAdapter=new UserAdapter(rootview.getContext(),list1);
                if (list==null||list.size()==0){
                    listView.setAdapter(null);
                }else {
                    listView.setAdapter(orderNoAdapter);
                }
                return true;
            }

            @Override
            public boolean onQueryTextChange(String newText) {
                List<OrderBean> list=OrderDao.getAllOrderByStaAndUserFinish(account,sta);
                List<OrderBean> list1=Tools.filterOrder(list,newText);
                UserAdapter orderNoAdapter=new UserAdapter(rootview.getContext(),list1);
                if (list==null||list.size()==0){
                    listView.setAdapter(null);
                }else {
                    listView.setAdapter(orderNoAdapter);
                }
                return true;
            }
        });

        return rootview;
    }


}